import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.util.Locale;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


import translator.TranslatableGUIElement; // for GUI elements
import translator.Translator;

public class StartFrame extends JFrame{
	final Translator translator = new Translator("lang/lang",Locale.GERMANY);
	private String path = "";
	JTextField  ta_y = new JTextField ();
	JTextField  ta_x = new JTextField ();
	JTextField  ta_spawn = new JTextField ();
	private int maxX=0, maxY=0, spawn=0;
	public boolean finish = false;
	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}
	/**
	 * @return the maxX
	 */
	public int getMaxX() {
		return maxX;
	}
	/**
	 * @return the maxY
	 */
	public int getMaxY() {
		return maxY;
	}
	/**
	 * @return the spawn
	 */
	public int getSpawn() {
		return spawn;
	}
	
	public StartFrame()
	{
		super("Menu");
		JPanel panel = new JPanel();		
		panel.setLocation(0, 20);
		JLabel lbl_info_open = new JLabel();
		lbl_info_open.setText(translator.translateMessage("StartFrameLoadInfoText"));		
		panel.add(lbl_info_open);
		JButton openfile = new JButton(translator.translateMessage("OpenFileDialog"));
		openfile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				path = OpenFileDialog();
				finish = true;
			}
		});
		panel.add(openfile);
		JPanel generatepnl = new JPanel();
		JLabel lbl_info_generate = new JLabel();
		lbl_info_generate.setText(translator.translateMessage("Generate"));
		generatepnl.add(lbl_info_generate);
		ta_x.setText(translator.translateMessage("GenerateLblInfoXCoord"));
		ta_x.setSize(10,20);
		ta_y.setSize(10,20);
		ta_y.setText(translator.translateMessage("GenerateLblInfoYCoord"));
		ta_spawn.setText(translator.translateMessage("GenerateLblInfoSpawns"));
		ta_spawn.setSize(10,20);
		JButton generateBtn = new JButton(translator.translateMessage("Generate"));
		generateBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				SetTextToVars();
				System.out.println(maxX + " " + maxY + " " + spawn);
				
			}
		});
		generatepnl.add(ta_spawn);
		JButton translateBtn = new JButton(translator.translateMessage("Translate"));
		translateBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(translator.getCurrentLocale() == Locale.GERMANY)
				{
				translator.setTranslatorLocale(Locale.US);
				}
				else
				{
					translator.setTranslatorLocale(Locale.GERMANY);
				}
				}
			}
		);
		
		panel.add(translateBtn);
		
		generatepnl.add(ta_x);
		generatepnl.add(ta_y);
		generatepnl.add(generateBtn);
		generatepnl.setLocation(0, 40);
		panel.add(generatepnl);
		
		ta_x.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent arg0) {
				ta_x.setText("");
			}

			@Override
			public void focusLost(FocusEvent e) {
				Integer casttemp = new Integer(ta_x.getText());
				maxX = casttemp; 
			}
});

		ta_y.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent arg0) {
				ta_y.setText("");
				
			}

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				Integer casttemp = new Integer(ta_y.getText());
				maxY = casttemp; 
			}
});

		ta_spawn.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent arg0) {
				ta_spawn.setText("");
				
			}

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				Integer casttemp = new Integer(ta_spawn.getText());
				spawn = casttemp; 
			}
});
		
		add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1200, 200);
		
	}
	 protected void SetTextToVars() {
		Controller cntr = new Controller(maxX, maxY, spawn);
		this.setVisible(false);
		finish = true;
		
		
	}
	protected String OpenFileDialog() {
		 JFileChooser jfcChooser = new JFileChooser();
	     int fileState = jfcChooser.showOpenDialog(null);
	     File file = jfcChooser.getSelectedFile();
	     if (file != null && fileState == JFileChooser.APPROVE_OPTION)
	     {
	         return(file.getAbsolutePath());
	     }
	     else if (fileState == JFileChooser.CANCEL_OPTION)
	     {
	         return "";
	     }
		return "";
	}
	 
	public static void main(String[] args)  {
         SwingUtilities.invokeLater(new Runnable() {

         public void run() {
             new StartFrame().setVisible(true);

         }

     });
}
}
